﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common;
using System.Data;
using TypedROBIN.Common;
using System.Collections;

namespace TypedROBIN.Imaging
{
    public class LogDao
    {
        private DaoHelper m_Helper = new DaoHelper("Logger");

        internal void Add(LogData data)
        {
            string commandText = "INSERT INTO [eventlog] ([logid],[timestamp],[loglevel],[message],[taskid],[calleridentity]) VALUES(@logid,@timestamp,@loglevel,@message,@taskid,@calleridentity)";
            DbParameter[] parameters = new DbParameter[6];
            parameters[0] = m_Helper.ConvertToParameter("logid",data.LogId);
            parameters[1] = m_Helper.ConvertToParameter("timestamp", data.TimeStamp, DbType.DateTime2);
            parameters[2] = m_Helper.ConvertToParameter("loglevel", data.LogLevel, DbType.Int32);
            parameters[3] = m_Helper.ConvertToParameter("message", data.Message);
            parameters[4] = m_Helper.ConvertToParameter("taskid", data.TaskId);
            parameters[5] = m_Helper.ConvertToParameter("calleridentity", data.CallerIdentity);
            m_Helper.ExecuteNonQuery(commandText, parameters);
        }

        internal void AddRange(LogData[] datas)
        {
            if (datas == null || datas.Length == 0)
            {
                return;
            }
            foreach (LogData data in datas)
            {
                Add(data);
            }
        }

        internal int Remove(Level level)
        {
            string commandText = "DELETE FROM [eventlog] WHERE [loglevel]=@loglevel";
            DbParameter[] parameters = new DbParameter[1];
            parameters[0] = m_Helper.ConvertToParameter("loglevel", level, DbType.Int32);
            return m_Helper.ExecuteNonQuery(commandText, parameters);
        }
        
        internal int Clear()
        {
            string commandText = "DELETE FROM [eventlog]";
            return m_Helper.ExecuteNonQuery(commandText);
        }

        internal LogData[] GetAll(Sort[] Sorts)
        {
            string commandText = "SELECT [logid],[timestamp],[loglevel],[message],[taskid],[calleridentity] FROM [eventlog]";
            if (Sorts != null && Sorts.Length > 0)
            {
                commandText += " ORDER BY ";
                for (int i = 0; i < Sorts.Length; i++)
                {
                    string s = Sorts[i].FieldName + " " + Sorts[i].Order.ToString() + ", ";
                    commandText += s;
                }
                if (Sorts.Length == 1)
                {
                    commandText = commandText.Substring(0, commandText.Length - 3);
                }
            }
            return HashtablesToLogDatas(m_Helper.ExecuteQuery(commandText));
        }

        private LogData[] HashtablesToLogDatas(Hashtable[] hashtables)
        {
            ArrayList datas = new ArrayList(0);
            if (hashtables != null && hashtables.Length > 0)
            {
                datas = new ArrayList(hashtables.Length);
                for (int i = 0; i < hashtables.Length; i++)
                {
                    LogData data = HashtableToLogData(hashtables[i]);
                    datas.Add(data);
                }
            }
            return datas.ToArray(typeof(LogData)) as LogData[];
        }

        private LogData HashtableToLogData(Hashtable hashtable)
        {
            return new LogData(
                (string) hashtable["logid"],
                (Level) hashtable["loglevel"],
                (DateTime) hashtable["timestamp"],
                (string) hashtable["message"],
                (string) hashtable["taskid"],
                (string) hashtable["calleridentity"]);
        }
    }
}
